package com.asia5b.a5mall.middleware.logic.viewmodel.member

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.base.SimpleNetParams
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.model.member.AddressModel
import com.asia5b.a5mall.middleware.model.member.RegionModel
import com.asia5b.a5mall.middleware.model.bean.TabRegionBean
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers

/**
 * Created by zhangyuncai on 2019/12/31.
 */
class SelectRegionDFVM :SimpleViewModel() {

    val renderRegionLiveData=MutableLiveData<ArrayList<RegionModel>>()

    /**
     * @Note   加载地址数据
     * @param  id 索引
     */
    fun loadRegion(id: Int) {
        val observable = baseApi.loadRegion(id)
        baseSubscribe(observable, SimpleNetParams(isShowLoading = false), Consumer { result ->
            if (result.size > 0) {
//                providerView().renderRegion(result, 0)
                renderRegionLiveData.value=result
            }
        })
    }

    /**
     * @Note   加载多个地址数据
     */
    fun loadAllRegion(tabDatas: ArrayList<TabRegionBean>, selectAddressModel: AddressModel) {

        val observableList = arrayListOf<Observable<ArrayList<RegionModel>>>()
        selectAddressModel.run {
            //请求国家列表
            observableList.add(baseApi.loadRegion(0))
            //请求州列表
            if (this.countryId > 0) {
                observableList.add(baseApi.loadRegion(this.countryId))
                //请求市列表
                if (this.provinceId > 0) {
                    observableList.add(baseApi.loadRegion(this.provinceId))
                    //请求区列表
                    if (this.cityId > 0) {
                        observableList.add(baseApi.loadRegion(this.cityId))
                    }
                }
            }
        }

        val subscribe = Observable.concatEager(observableList)
                .toList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe {
//                    loadMvpView.showLoadingBar("")
                    loadingLiveData.value=0
                }
                .doAfterTerminate {
//                    loadMvpView.dismissLoadingBar()
                    loadingLiveData.value=1
                }
                .subscribe(Consumer {
                    for ((index, result) in it.withIndex()) {
                        val tabRegionBean = TabRegionBean()
                        tabRegionBean.contentRegionModelList = result
                        if (result.size > 0) {
                            for (regionModel in result) {
                                if (index == 0) {//请求国家列表
                                    if (regionModel.id == selectAddressModel.countryId) {
                                        tabRegionBean.selectRegionModel = regionModel
                                    }
                                } else if (index == 1) { //请求州列表
                                    if (regionModel.id == selectAddressModel.provinceId) {
                                        tabRegionBean.selectRegionModel = regionModel
                                    }
                                } else if (index == 2) { //请求市列表
                                    if (regionModel.id == selectAddressModel.cityId) {
                                        tabRegionBean.selectRegionModel = regionModel
                                    }
                                } else if (index == 3) {//请求区列表
                                    if (regionModel.id == selectAddressModel.townId) {
                                        tabRegionBean.selectRegionModel = regionModel
                                    }
                                }
                                if (tabRegionBean.selectRegionModel != null) {
                                    break
                                }
                            }

                            tabDatas.add(tabRegionBean)

                        }
                    }
//                    providerView().loadAllRegionResult()
                    loadAllRegionResultLiveData.value=true
                }, Consumer {
                    it.printStackTrace()
                    /**
                     * todo 如果出错，重新加载所有国家列表
                     */
//                    providerView().loadRegionError()
                    loadRegionErrorLiveData.value=true
                })
        compositeDisposable.add(subscribe)
    }
    val loadAllRegionResultLiveData=MutableLiveData<Boolean>()
    val loadRegionErrorLiveData=MutableLiveData<Boolean>()

}